package com.briup.web.interceptor;

import com.briup.constant.SystemConstant;
import com.briup.enums.CodeAndMessageEnum;
import com.briup.exception.SystemException;
import com.briup.mapper.AccountMapper;
import com.briup.util.JWTUtils;
import com.briup.util.RedisUtils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.MalformedJwtException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

@Slf4j
@Component
public class CheckLoginInterceptor implements HandlerInterceptor {
    @Autowired
    AccountMapper accountMapper;
    @Autowired
    RedisUtils redisUtils;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("拦截到请求：{}",request.getRemoteAddr());
        if ("OPTIONS".equals(request.getMethod())) {
            return true;
        }
        String token = request.getHeader(SystemConstant.REQUEST_HEADER);
        //判断是否是预请求
        if (request.getMethod().equals(SystemConstant.PRE_REQUEST)) {
            return true;
        }
        if (Objects.isNull(token) || !StringUtils.hasText(token)) {
            throw new SystemException(CodeAndMessageEnum.USER_NOT_LOGIN);
        }
        Claims claims=null;
        try {
            claims = JWTUtils.paresToken(token);
        } catch (ExpiredJwtException e) {
            throw new SystemException(CodeAndMessageEnum.TOKEN_EXPIRED);
        } catch (MalformedJwtException e) {
            throw new SystemException(CodeAndMessageEnum.TOKEN_INVALID);
        }
        Integer status = (Integer) claims.get("status");
//        log.info("状态：{}",status);
        if (status!=1){
            throw new SystemException(CodeAndMessageEnum.USER_IS_BAN);
        }
        String accountID = (String) claims.get("accountId");
//        使用redis通过用户id获取jwt
        String redisJWT = (String) redisUtils.get(accountID);
        if (Objects.isNull(redisJWT) || !StringUtils.hasText(redisJWT)){
            throw new SystemException(CodeAndMessageEnum.TOKEN_INVALID);
        }
        if (!redisJWT.equals(token)){
            throw new SystemException(CodeAndMessageEnum.TOKEN_INVALID);
        }
        String username = (String) claims.get("username");
//        log.info("id{}",accountID);
        request.setAttribute("id",accountID);
        request.setAttribute("username",username);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
